package com.yunze.web.controller.yunze.install;

import com.alibaba.fastjson.JSON;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.ip.IpUtils;
import com.yunze.common.utils.yunze.AesEncryptUtil;
import com.yunze.system.service.yunze.install.YzInstallLinkmanService;
import com.yunze.web.core.config.MyBaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * 按警联系人操作
 * @author liuhang
 * @date 2022-6-8
 * */

@Api("按警联系人")
@RestController
@RequestMapping("/yunze/linkman")
public class YzInstallLinkmanController extends MyBaseController {

    @Autowired
    private YzInstallLinkmanService yzInstallLinkmanService;

    /**
     * 执行 按警联系人 列表
     * @param Pstr
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('yunze:linkman:list')")
    @PostMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public String SynFlow(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            //  System.out.println(map);
            Parammap.putAll(JSON.parseObject(Pstr));
            return MyRetunSuccess(yzInstallLinkmanService.ListMap(Parammap), null);
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:linkman:list  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.getCause().toString());
        }
        return Myerr("按警联系人[列表] 操作失败！");
    }


    /**
     * 执行 按警联系人 新增
     * @param Pstr
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('yunze:linkman:list')")
    @PostMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public String Add(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            //  System.out.println(map);
            Parammap.putAll(JSON.parseObject(Pstr));
            boolean bool = yzInstallLinkmanService.add(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！", null);
            }else {
                return Myerr("新增 按警联系人 操作失败！");
            }
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:linkman:add  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.getCause().toString());
        }
        return Myerr("按警联系人[新增] 操作失败！");
    }


    /**
     * 查询手机号  是否存在
     * @param Pstr
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('yunze:linkman:phone')")
    @PostMapping(value = "/phone", produces = {"application/json;charset=UTF-8"})
    public String PhoneLinkman(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            //  System.out.println(map);
            Parammap.putAll(JSON.parseObject(Pstr));
            boolean bool = yzInstallLinkmanService.phone(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("手机号码已存在");
            }
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:linkman:phone  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.getCause().toString());
        }
        return Myerr("查询手机号 操作失败！");
    }


    /**
     * 删除
     * @param Pstr
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('yunze:linkman:del')")
    @PostMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    public String deleteKman(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            //  System.out.println(map);
            Parammap.putAll(JSON.parseObject(Pstr));
            boolean bool = yzInstallLinkmanService.delId(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("删除失败");
            }
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:linkman:del  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.getCause().toString());
        }
        return Myerr("删除 操作失败！");
    }

    /**
     * 修改
     * @param Pstr
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('yunze:linkman:upd')")
    @PostMapping(value = "/upd", produces = {"application/json;charset=UTF-8"})
    public String UpdLinkman(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            //  System.out.println(map);
            Parammap.putAll(JSON.parseObject(Pstr));
            boolean bool = yzInstallLinkmanService.updForm(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("修改失败");
            }
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:linkman:upd  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.getCause().toString());
        }
        return Myerr("修改 操作失败！");
    }

}
